/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A class representing creation dialog on screen.
 * For example:
 * <pre>
 *    ElmIniEqNodalForcesDialog dgl = new ElmIniEqNodalForcesDialog(parent);
 *    dgl.pack();
 * </pre>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public final class ElmIniEqNodalForcesDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private FemModel fmodel;
	
	private String anlType;
	
	private DecimalField eqFx1Field;
	private DecimalField eqFy1Field;
	private DecimalField eqFz1Field;
	private DecimalField eqMx1Field;
	private DecimalField eqMy1Field;
	private DecimalField eqMz1Field;
	private DecimalField eqFx2Field;
	private DecimalField eqFy2Field;
	private DecimalField eqFz2Field;
	private DecimalField eqMx2Field;
	private DecimalField eqMy2Field;
	private DecimalField eqMz2Field;
	public double eqFx1;
	public double eqFy1;
	public double eqFz1;
	public double eqMx1;
	public double eqMy1;
	public double eqMz1;
	public double eqFx2;
	public double eqFy2;
	public double eqFz2;
	public double eqMx2;
	public double eqMy2;
	public double eqMz2;
	
//*****************************************************************************
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.InsaneInternalInterface
	 */
	public ElmIniEqNodalForcesDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		fmodel = parent.getModel().getDiscreteModel();
		
		if (this.parent.getController().getSelection().size() != 1 )
			anlType = fmodel.getGlobalAnalysisModel().getType();
		else {
			FrameElement elm = (FrameElement)this.parent.getController().getSelection().get(0);
			anlType = elm.getAnalysisModel().getType();
		}
		
		setUpFormat();
		setUpFields();
		
//		Sets up the initial equivalent nodal forces panel
		JPanel iniEqNForcesPane = new JPanel();
		iniEqNForcesPane.setPreferredSize(new Dimension(360,280));
		iniEqNForcesPane.setLayout(new BoxLayout(iniEqNForcesPane, BoxLayout.Y_AXIS));
		
		JPanel eqPicPane = new JPanel();
		eqPicPane.setMaximumSize(new Dimension(500,100));
		eqPicPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/IniEqForce100.gif")) ));
		iniEqNForcesPane.add(eqPicPane);
		
		JPanel eq1Pane = new JPanel();
		eq1Pane.setLayout(new GridLayout(2,3,2,2));
		eq1Pane.setMaximumSize(new Dimension(500,90));
		eq1Pane.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmElaConDialog01")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
			
		JPanel eqF1xPane = new JPanel();
		JPanel eqF1yPane = new JPanel();
		JPanel eqF1zPane = new JPanel();
		eqF1xPane.add(new JLabel("Fx:"));
		eqF1xPane.add(eqFx1Field);
		eqF1yPane.add(new JLabel("Fy:"));
		eqF1yPane.add(eqFy1Field);
		eqF1zPane.add(new JLabel("Fz:"));
		eqF1zPane.add(eqFz1Field);
		eq1Pane.add(eqF1xPane);
		eq1Pane.add(eqF1yPane);
		eq1Pane.add(eqF1zPane);
		
		JPanel eqM1xPane = new JPanel();
		JPanel eqM1yPane = new JPanel();
		JPanel eqM1zPane = new JPanel();
		eqM1xPane.add(new JLabel("Mx:"));
		eqM1xPane.add(eqMx1Field);
		eqM1yPane.add(new JLabel("My:"));
		eqM1yPane.add(eqMy1Field);
		eqM1zPane.add(new JLabel("Mz:"));
		eqM1zPane.add(eqMz1Field);
		eq1Pane.add(eqM1xPane);
		eq1Pane.add(eqM1yPane);
		eq1Pane.add(eqM1zPane);
		iniEqNForcesPane.add(eq1Pane);
		
		JPanel eq2Pane = new JPanel();
		eq2Pane.setLayout(new GridLayout(2,3,2,2));
		eq2Pane.setMaximumSize(new Dimension(500,90));
		eq2Pane.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmElaConDialog02")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
			
		JPanel eqF2xPane = new JPanel();
		JPanel eqF2yPane = new JPanel();
		JPanel eqF2zPane = new JPanel();
		eqF2xPane.add(new JLabel("Fx:"));
		eqF2xPane.add(eqFx2Field);
		eqF2yPane.add(new JLabel("Fy:"));
		eqF2yPane.add(eqFy2Field);
		eqF2zPane.add(new JLabel("Fz:"));
		eqF2zPane.add(eqFz2Field);
		eq2Pane.add(eqF2xPane);
		eq2Pane.add(eqF2yPane);
		eq2Pane.add(eqF2zPane);
		
		JPanel eqM2xPane = new JPanel();
		JPanel eqM2yPane = new JPanel();
		JPanel eqM2zPane = new JPanel();
		eqM2xPane.add(new JLabel("Mx:"));
		eqM2xPane.add(eqMx2Field);
		eqM2yPane.add(new JLabel("My:"));
		eqM2yPane.add(eqMy2Field);
		eqM2zPane.add(new JLabel("Mz:"));
		eqM2zPane.add(eqMz2Field);
		eq2Pane.add(eqM2xPane);
		eq2Pane.add(eqM2yPane);
		eq2Pane.add(eqM2zPane);
		iniEqNForcesPane.add(eq2Pane);
		
		setPane(iniEqNForcesPane);
		
    }
	
//*****************************************************************************
	
    public int showDialog() {
        return super.showDialog(parent, parent.getResourceBundle().getString("dElmElaConDialog04"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		
		try {
			
			eqFx1 = eqFx1Field.getValue();
			eqFy1 = eqFy1Field.getValue();
			eqFz1 = eqFz1Field.getValue();
			eqMx1 = eqMx1Field.getValue();
			eqMy1 = eqMy1Field.getValue();
			eqMz1 = eqMz1Field.getValue();
			eqFx2 = eqFx2Field.getValue();
			eqFy2 = eqFy2Field.getValue();
			eqFz2 = eqFz2Field.getValue();
			eqMx2 = eqMx2Field.getValue();
			eqMy2 = eqMy2Field.getValue();
			eqMz2 = eqMz2Field.getValue();
			
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		
		if (this.parent.getController().getSelection().size() == 1 ) {
			
			FrameElement elm = (FrameElement)this.parent.getController().getSelection().get(0);
			
			//Setting initial equivalent nodal forces fields
			eqFx1Field = new DecimalField(elm.getInitialNodalForce(0), 7, numberFormat);
			eqFy1Field = new DecimalField(elm.getInitialNodalForce(1), 7, numberFormat);
			eqFz1Field = new DecimalField(elm.getInitialNodalForce(2), 7, numberFormat);
			eqMx1Field = new DecimalField(elm.getInitialNodalForce(3), 7, numberFormat);
			eqMy1Field = new DecimalField(elm.getInitialNodalForce(4), 7, numberFormat);
			eqMz1Field = new DecimalField(elm.getInitialNodalForce(5), 7, numberFormat);
			eqFx2Field = new DecimalField(elm.getInitialNodalForce(6), 7, numberFormat);
			eqFy2Field = new DecimalField(elm.getInitialNodalForce(7), 7, numberFormat);
			eqFz2Field = new DecimalField(elm.getInitialNodalForce(8), 7, numberFormat);
			eqMx2Field = new DecimalField(elm.getInitialNodalForce(9), 7, numberFormat);
			eqMy2Field = new DecimalField(elm.getInitialNodalForce(10), 7, numberFormat);
			eqMz2Field = new DecimalField(elm.getInitialNodalForce(11), 7, numberFormat);
			
		} else {
			
			//Setting initial equivalent forces fields
			eqFx1Field = new DecimalField(0, 7, numberFormat);
			eqFy1Field = new DecimalField(0, 7, numberFormat);
			eqFz1Field = new DecimalField(0, 7, numberFormat);
			eqMx1Field = new DecimalField(0, 7, numberFormat);
			eqMy1Field = new DecimalField(0, 7, numberFormat);
			eqMz1Field = new DecimalField(0, 7, numberFormat);
			eqFx2Field = new DecimalField(0, 7, numberFormat);
			eqFy2Field = new DecimalField(0, 7, numberFormat);
			eqFz2Field = new DecimalField(0, 7, numberFormat);
			eqMx2Field = new DecimalField(0, 7, numberFormat);
			eqMy2Field = new DecimalField(0, 7, numberFormat);
			eqMz2Field = new DecimalField(0, 7, numberFormat);
			
		}
		
		//Enabling or disabling fields according to analysis model type
		if (this.parent.getController().getSelection().size() == 1)
			anlType = ((FrameElement)this.parent.getController().getSelection().get(0)).getAnalysisModel().getType();
		else anlType = fmodel.getGlobalAnalysisModel().getType();
		
		if (anlType.equals("Beam")) {
			
			eqFx1Field.setEnabled(false);
			eqFx2Field.setEnabled(false);
			eqFy1Field.setEnabled(true);
			eqFy2Field.setEnabled(true);
			eqFz1Field.setEnabled(false);
			eqFz2Field.setEnabled(false);
			eqMx1Field.setEnabled(false);
			eqMx2Field.setEnabled(false);
			eqMy1Field.setEnabled(false);
			eqMy2Field.setEnabled(false);
			eqMz1Field.setEnabled(true);
			eqMz2Field.setEnabled(true);
			
		} else if (anlType.equals("PlaneFrame")) {
			
			eqFx1Field.setEnabled(true);
			eqFx2Field.setEnabled(true);
			eqFy1Field.setEnabled(true);
			eqFy2Field.setEnabled(true);
			eqFz1Field.setEnabled(false);
			eqFz2Field.setEnabled(false);
			eqMx1Field.setEnabled(false);
			eqMx2Field.setEnabled(false);
			eqMy1Field.setEnabled(false);
			eqMy2Field.setEnabled(false);
			eqMz1Field.setEnabled(true);
			eqMz2Field.setEnabled(true);
			
		} else if (anlType.equals("PlaneTruss")) {
			
			eqFx1Field.setEnabled(true);
			eqFx2Field.setEnabled(true);
			eqFy1Field.setEnabled(true);
			eqFy2Field.setEnabled(true);
			eqFz1Field.setEnabled(false);
			eqFz2Field.setEnabled(false);
			eqMx1Field.setEnabled(false);
			eqMx2Field.setEnabled(false);
			eqMy1Field.setEnabled(false);
			eqMy2Field.setEnabled(false);
			eqMz1Field.setEnabled(false);
			eqMz2Field.setEnabled(false);
			
		} else if (anlType.equals("Grid")) {
			
			eqFx1Field.setEnabled(false);
			eqFx2Field.setEnabled(false);
			eqFy1Field.setEnabled(true);
			eqFy2Field.setEnabled(true);
			eqFz1Field.setEnabled(false);
			eqFz2Field.setEnabled(false);
			eqMx1Field.setEnabled(true);
			eqMx2Field.setEnabled(true);
			eqMy1Field.setEnabled(false);
			eqMy2Field.setEnabled(false);
			eqMz1Field.setEnabled(true);
			eqMz2Field.setEnabled(true);
			
		}
		
	}
	
//*****************************************************************************
}

